import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { HttpSeriveProvider } from "../http-serive/http-serive";
import { Observable } from "rxjs/Observable";
import * as b from "jsonp";
import { RequestOptions, Headers, RequestOptionsArgs } from "@angular/http";
/*
  Generated class for the TeamSeriveProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class TeamSeriveProvider {
  public headers = new Headers({
    'Content-Type': 'application/json'
  });

  mergeToken = (options: RequestOptionsArgs = {}) => {
    const newOptions = new RequestOptions({}).merge();
    const newHeaders = new Headers(newOptions.headers);

      newHeaders.set('Authorization', '1f51380a-e763-49fa-8260-122b768c3067');
    newOptions.headers = newHeaders;
    return newOptions;
  };

  constructor(
    public http: HttpClient,
    public httpSeriveProvider: HttpSeriveProvider
  ) {}

  /**
   * 临时登录接口 之后应该放在公用
   * @param password 密码
   * @param phone 手机号
   */ 
  login(password: string, phone: string): Observable<any> {
    const stringUser = JSON.stringify({ "phone": "13229677963", "password": "123" });
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}user/login`,
      stringUser,
      this.headers
    );
  }

  /**
   * 临时验证是否登录接口 之后应该放在公用
   * @param token 登录状态值 保存在本地
   */
  isLogin(token){
    const _token = JSON.stringify({ token });
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}check/token`,
      _token,
      this.headers
    );
  }

  /**
   *获取团队信息
   * @param teamCode 团队ID
   */
  getTeamDetails(teamCode: number, token): Observable<any> {
    
let headers = new Headers(); 
headers.set("Content-Type", "text/plain");
headers.set("Authorization", "123123");
headers.set("Token", token);
    let options = new RequestOptions({ headers: headers });
    return this.httpSeriveProvider.post(
      `http://112.74.200.88:18088/api/user/checkLogin`,
      {}, 
      options
    ); 
  }

  /**
   * 获取加入的团队
   */
  getJoinTeam(token): Observable<any> { 
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/jion/teams`
    );
  }

  /**
   *加入团队
   * @param teamcode 团队编号
   * @param usercode 使用者编号
   */
  joinTeam(teamcode: number, usercode: number): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}user/jion/team`,
      { teamcode, usercode }
    );
  }

  /**
   *创建团队
   * @param teamcode 团队名称
   * @param teamicon 团队头像 (需要先经过上传接口，再获取上传接口返回的字符串)
   */
  cerateTeam(teamname: number, teamicon: string) {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/create`,
      { teamname, teamicon }
    );
  }

  /**
   *判断团队名称是否重复
   * @param teamname 团队名称
   */
  getTeamNameRepeat(teamname: string) {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/check/name`,
      { teamname }
    );
  }
}
